import time
import unittest
import utils
import requests

from api.login import LoginApi
from api.recharge import RechargeApi
from api.trust_account import TrustAccountApi


class TestRecharge(unittest.TestCase):

    @classmethod
    def setUpClass(cls) -> None:
        cls.login_api = LoginApi()
        cls.trust_account = TrustAccountApi()
        cls.recharge_api = RechargeApi()

    def setUp(self) -> None:
        self.session = requests.Session()
        self.login_api.login(self.session, "13012345678", "test123")

    def tearDown(self) -> None:
        self.session.close()

    # 充值成功
    def test01_recharge_success(self):
        # 测试数据
        amount = 300000
        valicode = 8888

        # 调用接口
        r1 = self.recharge_api.get_recharge_img_verify(self.session)
        self.assertEqual(200, r1.status_code)

        # 充值
        r2 = self.recharge_api.recharge(self.session, amount, valicode)
        print("recharge data=", r2.json())
        utils.common_assert(self, r2, 200, 200, None)

        # 提取表单数据
        html = r2.json().get("description").get("form")
        print("html====", html)
        action, form_data = utils.parse_html(html)

        # 调用第三方充值接口
        r3 = requests.post(action, data=form_data)
        print("第三方充值接口返回数据：", r3.text)
        self.assertEqual(200, r3.status_code)
